﻿using com.nm.ApiServer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using com.nm.Attributes;
using com.nm.ApiServer.Attributes;
using george.Data;
using com.nm.db.Models.Systems;
using Nancy.Extensions;

namespace BaseAPI
{
    [Description("系统设置")]
    public class SetHandler : BaseHandler
    {
        [MustLogin]
        [Description("获取系统设置列表")]
        [ReturnType(typeof(model.DictData.PageDictList))]
        public ApiResult GetConfigList()//[ParamDescription("用户ID")]int UserID)
        {
            try
            {
                var dc = new SysEntities();
                var query = (from d1 in dc.sys_dictionary
                             join c in dc.sys_config on d1.dictValue equals c.dictValue
                             join d2 in dc.sys_dictionary on d1.ParentId equals d2.ID
                             where d1.SystemIdentifier == true && d2.dictValue == "0"
                             select
                             new
                             {
                                 d1, //d2,
                                 c.ControlType,
                                 c.Value,
                                 c.isRemove,
                             });
                var queryChild= (from d1 in dc.sys_dictionary
                                 join c in dc.sys_config on d1.dictValue equals c.dictValue
                                 join d2 in dc.sys_dictionary on d1.ID equals d2.ParentId
                                 where d1.SystemIdentifier == true
                                 select d2);
                var result = query.Select(r => new model.DictData.SysDictList
                {
                    ID = r.d1.ID,
                    DictName = r.d1.dictName,
                    DictValue = r.d1.dictValue,
                    ControlType = r.ControlType ?? 0,
                    Value = r.Value,
                    ConfigIsRemove = r.isRemove ?? false,
                }).DistinctBy(r => r.ID).ToList();
                foreach (var item in result)
                {
                    item.Childrens = queryChild.Where(r => r.ParentId == item.ID).Select(d => new model.DictData.PageDictList
                    {
                        ID = d.ID,
                        ParentId = d.ParentId,
                        DictName = d.dictName,
                        DictValue = d.dictValue,
                    }).ToList();
                }
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        [MustLogin]
        [Description("获取系统字典列表")]
        [ReturnType(typeof(model.DictData.SysDictData))]
        public ApiResult GetDicList()//[ParamDescription("用户ID")]int UserID)
        {
            try
            {
                var dc = new SysEntities();
                var query = (from d1 in dc.sys_dictionary
                                 //join d2 in dc.sys_dictionary on d1.ID equals d2.ParentId
                             where d1.SystemIdentifier == true && d1.ParentId == 5 &&!(
                                from c in dc.sys_config 
                                select c.dictValue
                             ).Contains(d1.dictValue)
                             select d1).DistinctBy(d => d.ID);
                var result = query.Select(r => new model.DictData.SysDictData
                {
                    ID = r.ID,
                    DictValue = r.dictValue,
                    DictName = r.dictName,
                }).ToList();
                return Success(result);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        [JsonType]
        [MustLogin]
        [Description("新增系统设置信息")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult CreatConfig([ParamDescription("输入参数")]model.ConfigData.SysConfigData param)
        {
            try
            {
                if (string.IsNullOrEmpty(param.dictValue)) return Error("字典值不能为空！");
                var dc = new SysEntities();
                if (dc.sys_config.Any(t => t.dictValue == param.dictValue)) return Error($"字典值【{param.dictValue}】已存在");
                var config = new sys_config()
                {
                    dictValue = param.dictValue,
                    ControlType = param.ControlType,
                    isRemove = param.IsRemove,
                };
                dc.sys_config.Add(config);
                dc.SaveChanges();
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        [JsonType]
        [MustLogin]
        [Description("修改系统设置信息")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult ModifyConfig([ParamDescription("输入参数")]model.ConfigData.SysConfigData param)
        {
            try
            {
                if (string.IsNullOrEmpty(param.dictValue)) return Error("字典值不能为空！");
                var dc = new SysEntities();
                var query = dc.sys_config.FirstOrDefault(t => t.dictValue == param.dictValue);
                if (query != null)
                {
                    var conType = query.ControlType;
                    query.ControlType = param.ControlType;
                    query.isRemove = param.IsRemove;
                    if (conType == 4&& conType!= param.ControlType)
                    {
                        query.Value = null;
                    }
                }
                dc.SaveChanges();
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }

        [JsonType]
        [MustLogin]
        [Description("修改系统设置信息")]
        [ReturnType(typeof(int), "0-成功")]
        public ApiResult ModifyByDictValue([ParamDescription("输入参数")]model.ConfigData.SysConfigData param)
        {
            try
            {
                if (string.IsNullOrEmpty(param.dictValue)) return Error("字典值不能为空！");
                var dc = new SysEntities();
                var query = dc.sys_config.FirstOrDefault(t => t.dictValue == param.dictValue);
                if (query != null) query.Value = param.Value;
                dc.SaveChanges();
                return Success(0);
            }
            catch (Exception ex)
            {
                return Error(ex.Message);
            }
        }
    }
}
